using Magiq.Insert;
using Magiq.Providers.Objects;

namespace Magiq.Databases.SqlServer
{
    public class BulkInsertStrategy<TSource, TDestination, TNew> : IInsertStrategy, IDatabaseBasedExecution
        where TDestination : class
        where TNew : class, TDestination, new()
    {
        private readonly IInsert<TSource, TDestination, TNew> insert;

        public BulkInsertStrategy(IOrmAdapter ormAdapter, IInsert<TSource, TDestination, TNew> insert)
        {
            this.insert = insert;
            OrmAdapter = ormAdapter;
            BatchSize = 0;
            Timeout = 30;
        }

        public int BatchSize { get; set; }
        public int Timeout { get; set; }

        public int Execute()
        {
            var destination = new BulkCopyDestination<TSource, TDestination, TNew>(insert, OrmAdapter)
                                  {
                                      BatchSize = BatchSize,
                                      Timeout = Timeout
                                  };

            var objectsStrategy = new InsertStrategy<TSource, TDestination, TNew>(insert, destination);
            return objectsStrategy.Execute();
        }

        public IOrmAdapter OrmAdapter { get; private set; }
    }
}